﻿
package extremefx.ui {
	import extremefx.anim.Tween;
	import extremefx.events.EventArgs;		

	/**
	 * @author Marcelo Volmaro
	 */
	public class AnimatedComponent extends UIComponent {
		private var _animTween:Tween;
		public var _internalWidth:uint;
		public var _internalHeight:uint;
		public var _internalX:int;
		public var _internalY:int;
		 
		public var animationDuration:Number = .5;

		public function AnimatedComponent(){
			super();
		}
		
		private function _initAnimation():void {
			if(_animTween){
				_animTween.paused = true;
	
			} else {
				_animTween = new Tween(this, animationDuration);
				_animTween.onChange.add(_animate);
				_animTween.onComplete.add(_animate);
				_animTween.round = {
					"_internalWidth":true,
					"_internalHeight":true,
					"_internalX":true,
					"_internalY":true
				};
			}
		}
		
		private function _animate(pSender:Tween, pArgs:EventArgs):void {
			setBounds(_internalX, _internalY, _internalWidth, _internalHeight);
		}
		
		public function animateSize(pWidth:uint, pHeight:uint):void {
			_initAnimation();
			
			_internalWidth = _width;
			_internalHeight = _height;
			
			_animTween.setProperty("_internalWidth", pWidth);
			_animTween.setProperty("_internalHeight", pHeight);
		}
		
		public function animatePosition(pX:int, pY:int):void {
			_initAnimation();
			
			_internalX = _x;
			_internalY = _y;

			_animTween.setProperty("_internalX", pX);
			_animTween.setProperty("_internalY", pY);
		}
		
		override public function dispose():void {
			super.dispose();
			_animTween.dispose();
		}
	}
}
